import pydirectinput as pdi
import pyautogui as pyg
import pygetwindow as gw
import time
import os
from Tools import Tools
import keyboard
from RecordActionUtils import RecordActionUtils

pause = True
DNF_WD = gw.getWindowsWithTitle("Dungeon & Fighter")[0]
DNF_REGION = (DNF_WD.left, DNF_WD.top, DNF_WD.left + DNF_WD.width + 50, DNF_WD.top + DNF_WD.height + 50)
INIT_WIDTH = 800
INIT_HEIGHT = 600
ACTION_NAME = "夺回西部线"
tools = Tools(DNF_WD, init_width=INIT_WIDTH, init_height=INIT_HEIGHT)

out_image_pt=""
def abyss_xibu():
    global pause
    cnt = 1
    data = Tools.load_yaml("config/xibu-config.yml")
    print(data)
    while True:
        if pause:
            time.sleep(1)
            cnt = 1
        else:
            if cnt <= 1:
                print(f"{ACTION_NAME}开始深渊")
                if not tools.into_abyss(data["name"], data["into"]["unselect_image"], data["into"]["select_comm_image"],
                                        data["into"]["select_dy_image"], auto_into=True):
                    pause = True
            is_big_niu = tools.is_big_abyss(data["abyss"]["big_niu_image"], data["abyss"]["small_niu_image"],
                                                max_retry=50)
            global  out_image_pt
            out_image_pt = data["out_image"]["pt"]
            out_image_boss = data["out_image"]["boss"]
            finish_image = data["out_image"]["finish"]
            if not pause:
                tools.house_action("house1", is_big_niu, out_image_pt, out_image_boss, finish_image,
                                   clean_house=house1, out_house=out_house, reclean=reclean_house)
            if not pause:
                tools.house_action("house2", is_big_niu, out_image_pt, out_image_boss, finish_image,
                                   clean_house=house2, out_house=out_house, reclean=reclean_house)
            if not pause:
                tools.house_action("house3", is_big_niu, out_image_pt, out_image_boss, finish_image,
                                   clean_house=house3, out_house=out_house, reclean=reclean_house)
            if not pause:
                tools.house_action("house4", is_big_niu, out_image_pt, out_image_boss, finish_image,
                                   clean_house=house4, out_house=out_house, reclean=reclean_house)
            if not pause:
                tools.house_action("house5", is_big_niu, out_image_pt, out_image_boss, finish_image,
                                   clean_house=house5, out_house=out_house, reclean=reclean_house)
            if not pause:
                tools.house_action("house6", is_big_niu, out_image_pt, out_image_boss, finish_image,
                                   clean_house=house6, out_house=out_house, reclean=reclean_house)
            if not pause:
                tools.house_action("house7", is_big_niu, out_image_pt, out_image_boss, finish_image,
                                   clean_house=house7, out_house=None, reclean=reclean_house)
            if not pause:
                time.sleep(2)
                pdi.press("f10")
                time.sleep(5)
            if cnt > 13:
                # 回城吃疲劳
                if not pause:
                    time.sleep(10)
                    pdi.press("F12")
                    time.sleep(5)
                    pdi.press("3")
                    # into_shenyuan()
            cnt = cnt + 1


def clean_house():
    tools.hold_key(0.2, "right")
    pdi.press("w")


def house_inner_action(left_house_image, before_boss_image):
    # 进入房间后进行 遇见怪物，并释放几技能
    for i in range(0, 50):
        pdi.press("right")
        pdi.keyDown("right")
        time.sleep(0.2)
        pdi.keyUp("right")
        tools.release_key("w", 0.2)
        if tools.find_img_rel_path(left_house_image) or tools.find_img_rel_path(before_boss_image):
            tools.release_key("w", 0.2)


def house1():
    print("开始清理房间1")
    tools.release_key("w", 0.2)
    tools.hold_key(1, "right")
    tools.hold_key(0.3, "down")
    tools.hold_key(0.1, "up")
    tools.release_key("w", 0.2)
    tools.hold_key(1.5, "right")


def reclean_house():
    tools.release_key("w", 0.2)


def out_house():
    print("执行out house 方法 ")
    tools.hold_key(0.5, "right")


def house2():
    print("开始清理房间2")
    tools.hold_key(0.1, "right")
    tools.release_key("w", 0.3)
    tools.hold_key(1.7, "right")


def house3():
    print("开始清理房间3")
    tools.hold_key(0.1, "right")
    tools.release_key("w", 0.2)
    tools.hold_key(1.55, "right")


def house4():
    print("开始清理房间4")
    tools.hold_key(0.1, "right")
    tools.release_key("w", 0.2)
    tools.hold_key(1.4, "right")
    tools.release_key("w", 0.2)
    tools.hold_key(1, "right")


def house5():
    print("开始清理房间5")
    tools.hold_key(0.1, "right")
    tools.release_key("w", 0.3)
    tools.hold_key(1.4, "right")
    pdi.press("left")
    tools.release_key("e", 1)
    tools.release_key("s", 0.2)
    time.sleep(2.5)
    tools.release_key("w", 0.5)
    time.sleep(2.5)
    tools.release_key("w", 0.5)
    tools.hold_key(0.01, "down")
    tools.hold_key(0.1, "right")
    tools.release_key("w", 0.2)

    # 出售物品
    pdi.keyDown("alt")
    pdi.press("s")
    pdi.keyUp("alt")
    # 修理物品
    pdi.keyDown("alt")

    pdi.press("w")
    pdi.keyUp("alt")

    if not tools.find_img_rel_path(out_image_pt):
        tools.hold_key(0.05, "right")
        if not tools.find_img_rel_path(out_image_pt):
            tools.hold_key(0.05, "left")


def house6():
    print("开始清理房间6")
    #tools.hold_key(0.01, "up")
    tools.hold_key(0.1, "right")
    tools.release_key("w", 0.2)
    tools.hold_key(1.4, "right")
    tools.release_key("w", 0.2)
    tools.hold_key(0.015, "down")
    tools.hold_key(0.4, "right")


def house7():
    print("开始清理boss房间")
    tools.hold_key(0.2, "right")
    tools.release_key("w", 0.2)
    time.sleep(10)
    for i in range(0, 20):
        if tools.find_img_rel_path("jiabaili.png"):
            pdi.press("esc")
            break
        elif tools.find_img_rel_path("no_jiabaili.png"):
            break
        else:
            time.sleep(1)


def is_pause():
    global pause
    while pause:
        time.sleep(1)


def pause_function():
    global pause
    pause = True
    print(f"暂停{ACTION_NAME}")


def start():
    global pause
    pause = False
    print(f"开始{ACTION_NAME}")


if __name__ == '__main__':
    keyboard.add_hotkey("ctrl+f5", start)
    keyboard.add_hotkey("ctrl+f6", pause_function)
    keyboard.add_hotkey("ctrl+num_1", house1)
    keyboard.add_hotkey("ctrl+num_2", house2)
    keyboard.add_hotkey("ctrl+num_3", house3)
    keyboard.add_hotkey("ctrl+num_4", house4)
    keyboard.add_hotkey("ctrl+num_5", house5)
    keyboard.add_hotkey("ctrl+num_6", house6)
    keyboard.add_hotkey("ctrl+num_0", out_house)
    print(f"1,请按ctrl+f5开始 ，ctrl+f6停止深渊")
    abyss_xibu()
